package com.example.demo.controller;

import com.example.demo.UTF2GBK;
import com.example.demo.entity.*;
import com.example.demo.mapper.UserMapper;
import lombok.Data;
import lombok.SneakyThrows;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api")
public class Controller {

    @Resource
    UserMapper userMapper;
    static String name;

    @Data
    static class OnlyStatus{
        int status_code;
        @Override
        public String toString(){
            return "{\"status_code\":"+status_code+"}";
        }
    }

    @Data
    static class SignUp{
        String nickName;
        String phone;
        String password;
        @Override
        public String toString(){
            return "{nickName:'"+nickName+"'," +
                    "phone:'"+phone+"'," +
                    "password:'"+password+"'}";
        }
    }
    @Data
    static class SignIn{
        String phone;
        String password;
    }

    @Data
    static class SearchTable{
        String tablename;
    }
    @Data
    static class Phone{
        String phone;
    }


    @Data
    static class IdPhone{
        String phone;
        String user_id;
    }

    @PostMapping("/signUp")
    public String signUp(@RequestBody SignUp signJson){
        OnlyStatus success = new OnlyStatus();
        success.status_code = 0;
        return success.toString();
    }

    @PostMapping("/signIn")
    public String signUp(@RequestBody SignIn signJson){
        OnlyStatus success = new OnlyStatus();
        success.status_code = 0;
        return success.toString();
    }

    @PostMapping("/search")
    public String searchTable(@RequestBody SearchTable table) {

        int statue_code = 0;
        String all = userMapper.selectTable(table.tablename);
        return all;
//        String ans = "";
//
//
//        ""{\n" +
//                "\"status_code\":0,\n" +
//                "\"head\":[\"order_id\"],\n" +
//                "\"data\":[\n" +
//                "   {\"order_id\":\"1001\"},\n" +
//                "],\n" +
//                "}";
//        return ans;

    }

    @GetMapping("/test")
    public String test(){
        return "success";
    }

    @GetMapping( "/find_all_accept_info")
    public List<AcceptInfo> find_all_accept_info(){return userMapper.find_all_accept_info(); }

    @GetMapping( "/find_all_courier_basic_info")
    public List<CourierBasicInfo> find_all_courier_basic_info(){
        return userMapper.find_all_courier_basic_info();
    }

    @GetMapping( "/find_all_courier_info")
    public List<CourierInfo> find_all_courier_info(){
        return userMapper.find_all_courier_info();
    }

    @GetMapping( "/find_all_order_basic_info")
    public List<OrderBasicInfo> find_all_order_basic_info(){
        return userMapper.find_all_order_basic_info();
    }

    @GetMapping( "/find_all_send_info")
    public List<SendInfo> find_all_send_info(){

        System.out.println(userMapper.find_all_send_info());
        return userMapper.find_all_send_info();
    }

    @GetMapping( "/find_all_user_basic_info")
    public List<UserBasicInfo> find_all_user_basic_info(){
        return userMapper.find_all_user_basic_info();
    }

    @GetMapping( "/find_all_user_common_info")
    public List<UserCommonInfo> find_all_user_common_info(){
        return userMapper.find_all_user_common_info();
    }

    @PostMapping( "/find_last_user_common_info")
    public UserCommonInfo find_last_user_common_info(){
        return userMapper.find_last_user_common_info();
    }



    @PostMapping("/find_my_user_common_info")
    public List<UserCommonInfo> find_my_user_common_info(@RequestBody Phone myphone){
        return userMapper.find_my_user_common_info(myphone.phone);
    }

    @PostMapping("/find_my_accept_info")
    public List<AcceptInfo> find_my_accept_info(@RequestBody Phone myphone){
        return userMapper.find_my_accept_info(myphone.phone);
    }
    @PostMapping("/find_my_send_info")
    public List<SendInfo> find_my_send_info(@RequestBody Phone myphone){
        return userMapper.find_my_send_info(myphone.phone);
    }



    @PostMapping("/delete_my_user_common_info")
    public int delete_my_user_common_info(@RequestBody IdPhone idPhone){
        return userMapper.delete_my_user_common_info(idPhone.user_id,idPhone.phone);
    }

    @PostMapping("/update_my_user_common_info")
    public int update_my_user_common_info(@RequestBody UserCommonInfo userCommonInfo){
        return userMapper.update_my_user_common_info(userCommonInfo);
    }


    @PostMapping("/insert_my_user_common_info")
    public int insert_my_user_common_info(@RequestBody UserCommonInfo userCommonInfo){
//        userCommonInfo.setUser_common_name(UTF2GBK.utf82gbk(userCommonInfo.getUser_common_name()));
//        userCommonInfo.setUser_common_address(UTF2GBK.utf82gbk(userCommonInfo.getUser_common_address()));
//
//        System.out.println(userCommonInfo.getUser_common_address());
//        System.out.println(userCommonInfo.getUser_common_name());
        return userMapper.insert_my_user_common_info(userCommonInfo);
    }

    @PostMapping("/get_user_id")
    public String get_user_id(@RequestBody Phone myphone){
        return userMapper.get_user_id(myphone.phone);
    }
}
